package com.beiing.tianshuai.tianshuai.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.VersionInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/11/24 0024
 */

public class MainModel implements MainModelImpl {

    private static final String TAG = "MainModel";
    private OnMainResponseListener mListener;

    public MainModel(OnMainResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void getVersionInfo() {
        Observable<VersionInfoBean> response =
                DataManager.getVersionInfo();
        Observable<VersionInfoBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<VersionInfoBean>() {
            @Override
            public void onNext(@NonNull VersionInfoBean bean) {
                if (mListener != null) {
                    mListener.onGetVersionInfoSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    @Override
    public void getLoginState(String uid) {
        Observable<CodeBean> response =
                DataManager.getLoginState(uid);
        Observable<CodeBean> observable = response.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onGetLoginStateSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("===================== onComplete =======================", TAG);
            }
        });
    }

    public interface OnMainResponseListener {
        void onGetVersionInfoSuccess(VersionInfoBean bean);
        void onError(Throwable throwable);
        void onGetLoginStateSuccess(CodeBean bean);
    }
}
